package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.bean.bo.GoodsCreateBO;
import com.cskaoyan.mall.bean.bo.GoodsUpdateBO;
import com.cskaoyan.mall.bean.vo.*;
import com.cskaoyan.mall.service.BrandService;
import com.cskaoyan.mall.service.CategoryService;
import com.cskaoyan.mall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * WhereUse:后台  商品管理
 * description:
 * date: 2021/1/10 13:21
 * Created By YiFangWan
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {
  @Autowired
  GoodsService goodsService;

  @Autowired
  BrandService brandService;

  @Autowired
  CategoryService categoryService;



  /**
   * @WhereUse  后台，商品列表
   */
  @GetMapping("list")
  public ResultVO getGoodsList(Integer page, Integer limit, String goodsSn, String name, String sort, String order){
    GoodsListVO goodsList = goodsService.getGoodsList(page, limit,goodsSn,name, sort, order);
    return ResultVO.ok(goodsList);
  }

  /**
   * @WhereUse 商品列表-->商品删除
   */
  @PostMapping("delete")
  public ResultVO deleteGoods(@RequestBody GoodsItemsVO goodsItemsVO){
    int delete = goodsService.deleteGoods(goodsItemsVO);
    if (delete == 1){
      return ResultVO.ok();
    }
    return ResultVO.error(500,"意外的删除失败 -- HTY");
  }

  /**
   *
   * @WhereUse 编辑商品，种类 和 品牌 提供全部的数据
   *           从商场管理模块获取接口
   */
  @GetMapping("catAndBrand")
  public ResultVO getCatAndBrand(){
    List<GoodsCategoryVO> goodsCategoryVOS = categoryService.queryAllGoodsCategory(); //get Category
    List<GoodsBrandVO> goodsBrandVOS = brandService.queryAllGoodsBrands(); //getBrand
    GoodsCatAndBrandVO goodsCatAndBrandVO = new GoodsCatAndBrandVO(goodsCategoryVOS, goodsBrandVOS);
    return ResultVO.ok(goodsCatAndBrandVO);
  }

  /**
   * @WhereUse 编辑商品，回显商品信息，通过商品Id获得信息
   * @param id goods表Id
   */
  @GetMapping("detail")
  public ResultVO getDetail(Integer id){
    GoodsDetailVO goodsDetail = goodsService.getGoodsDetail(id);
    return ResultVO.ok(goodsDetail);
  }

  /**
   * @WhereUse 更新商品
   * @param goodsUpdateBO
   * @return
   */
  @PostMapping("update")
  public ResultVO updateGoods(@RequestBody GoodsUpdateBO goodsUpdateBO){
    int update = goodsService.updateGoods(goodsUpdateBO);
    if (update == 1)return ResultVO.ok();
    return ResultVO.error(500,"意外的更新错误 - HTY");
  }

  @PostMapping("create")
  public ResultVO createGoods(@RequestBody GoodsCreateBO goodsCreateBO){
    int insert = goodsService.insertGoods(goodsCreateBO);
    if (insert == 1) return ResultVO.ok();
    return ResultVO.error(500,"意外的新增错误 - HTY");
  }
}


